package com.example.terrorsms.model;

import java.util.Calendar;
import java.util.Date;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

import com.example.terrorsms.MainActivity;

public class EventsDataSource {
	private static final String TAG = "EventDataSource";

	// Database fields
	private SQLiteDatabase database;
	private final MySQLiteHelper dbHelper;
	@SuppressWarnings("unused")
	private final String[] allColumns = { MySQLiteHelper.COLUMN_ID,
			MySQLiteHelper.COLUMN_DATE, MySQLiteHelper.COLUMN_PHONENUMBER,
			MySQLiteHelper.COLUMN_MESSAGE, MySQLiteHelper.COLUMN_SENT };

	public EventsDataSource(Context context) {
		dbHelper = new MySQLiteHelper(context);
		open();
	}

	public void open() throws SQLException {
		database = dbHelper.getWritableDatabase();
	}

	public void close() {
		dbHelper.close();
	}

	public ScheduledEvent getNextEvent() {
		Cursor cursor = database.rawQuery(
				"SELECT * FROM " + MySQLiteHelper.TABLE_EVENTS
						+ " WHERE date >= " + System.currentTimeMillis()
						+ " AND " + MySQLiteHelper.COLUMN_SENT + " = 0"
						+ " ORDER BY date LIMIT 1", null);
		Log.d(TAG, "Suche Zeit " + System.currentTimeMillis());
		return cursorToScheduledEvent(cursor);
	}

	public ScheduledEvent getNextSendeableEvent() {
		Cursor cursor = database.rawQuery("SELECT * FROM "
				+ MySQLiteHelper.TABLE_EVENTS + " WHERE "
				+ MySQLiteHelper.COLUMN_SENT + " = 0"
				+ " ORDER BY date ASC LIMIT 1", null);
		return cursorToScheduledEvent(cursor);
	}

	/**
	 * 
	 * @param event
	 */
	public int countSentEvents() {
		return 0;
	}

	public void markEvent(ScheduledEvent event) {
		if (event != null) {
			database.rawQuery(
					"UPDATE " + MySQLiteHelper.TABLE_EVENTS + " SET "
							+ MySQLiteHelper.COLUMN_SENT + "= 1 WHERE _id = "
							+ event.getId(), null);
			Log.d(TAG, "Event marked");
		} else {
			Log.d(TAG, "Event is null, cannot be marked!");
		}
	}

	/**
	 * 
	 * @param event
	 *            ScheduledEvent
	 */
	public void createEvent(ScheduledEvent event) {
		ContentValues values = new ContentValues();
		Calendar cal = Calendar.getInstance();
		long targettime = 0;
		switch (MainActivity.EVENT_TYPE) {
		default:
		case MainActivity.TYPE_OWN:
			targettime = event.getDate().getTime();
			break;
		case MainActivity.TYPE_CHRISTMAS:
			cal.set(cal.get(Calendar.YEAR), Calendar.DECEMBER, 24, 0, 0, 0);
			targettime = cal.getTimeInMillis();
			break;
		case MainActivity.TYPE_NEWYEAREVE:
			cal.set(cal.get(Calendar.YEAR) + 1, Calendar.JANUARY, 31, 0, 0, 0);
			targettime = cal.getTimeInMillis();
			break;
		}
		values.put(MySQLiteHelper.COLUMN_DATE, targettime);
		values.put(MySQLiteHelper.COLUMN_PHONENUMBER, event.getPhoneNumber());
		values.put(MySQLiteHelper.COLUMN_MESSAGE, event.getMessage());
		values.put(MySQLiteHelper.COLUMN_SENT, 0);

		long insertId = database.insert(MySQLiteHelper.TABLE_EVENTS, null,
				values);
		Log.d(TAG, "Inserted ID: " + insertId);
	}

	/**
	 * 
	 * @param cursor
	 *            Cursor
	 * @return ScheduledEvent
	 */
	private ScheduledEvent cursorToScheduledEvent(Cursor cursor) {
		ScheduledEvent event = null;
		if (cursor.moveToFirst()) {
			event = new ScheduledEvent();
			event.setId(cursor.getInt(0));
			event.setDate(new Date(cursor.getLong(1)));
			event.setPhoneNumber(cursor.getString(2));
			event.setMessage(cursor.getString(3));
			event.setSent(cursor.getInt(4));
		}
		return event;
	}

}